/*
 * Copyright (c) 2010, okazoh_tk. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 *  - Redistributions of source code must retain the above copyright notice,
 *    this list of conditions and the following disclaimer.
 *  - Redistributions in binary form must reproduce the above copyright notice,
 *    this list of conditions and the following disclaimer in the documentation
 *    and/or other materials provided with the distribution.
 *  - Neither the name of the copyright holder nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#include "alkes/gfx/VertexBufferHelper.h"

namespace alkes {

VertexBufferHelper::VertexBufferHelper(uint32_t vertex_type)
: IVertexBuffer(vertex_type)
{
}

VertexBufferHelper::~VertexBufferHelper()
{
}

int32_t VertexBufferHelper::getVertexSize(const VertexTypeTrait* traits, int32_t length, uint32_t vertex_type)
{
    int32_t size = 0;
    int32_t i;

    for (i = 0; i < length; ++i, ++traits)
    {
        if (traits->type & vertex_type)
            size += traits->size;
    }

    return size;
}

int32_t VertexBufferHelper::getOffsetOf(VertexType member)
{
    int32_t length;
    const VertexTypeTrait* table = getVertexTypeTraits(length);
    if (!table)
        return -1;

    int32_t offset = 0;
    int32_t i;
    uint32_t vertex_type = getVertexType();

    for (i = 0; i < length; ++i, ++table)
    {
        if (table->type & member)
            return offset;
        else if (table->type & vertex_type)
            offset += table->size;
    }

    LOGE("%d is unknown VertexType.", member);
    return -1;
}

bool VertexBufferHelper::onSetVertex2D(int32_t index, int32_t count, const Vector2D* vertex)
{
    CopyVertexHelper<Vector2D, Vector2D2Vector3D>::setVertex(
        getBuffer(index, count), getOffsetOf(AL_VERTEX_XY), getVertexSize(), vertex, count);
    return true;
}

bool VertexBufferHelper::onSetVertex3D(int32_t index, int32_t count, const Vector3D* vertex)
{
    CopyVertexHelper<Vector3D>::setVertex(getBuffer(index, count), getOffsetOf(AL_VERTEX_XYZ), getVertexSize(), vertex, count);
    return true;
}

bool VertexBufferHelper::onSetNormal(int32_t index, int32_t count, const Vector3D* vertex)
{
    CopyVertexHelper<Vector3D>::setVertex(getBuffer(index, count), getOffsetOf(AL_VERTEX_NORMAL), getVertexSize(), vertex, count);
    return true;
}

bool VertexBufferHelper::onSetDiffuse(int32_t index, int32_t count, const Color* color)
{
    CopyVertexHelper<Color>::setVertex(getBuffer(index, count), getOffsetOf(AL_VERTEX_COLOR), getVertexSize(), color, count);
    return true;
}

bool VertexBufferHelper::onSetSpecular(int32_t index, int32_t count, const Color* color)
{
    CopyVertexHelper<Color>::setVertex(getBuffer(index, count), getOffsetOf(AL_VERTEX_SPECULAR), getVertexSize(), color, count);
    return true;
}

bool VertexBufferHelper::onSetTexCoord(int32_t index, int32_t count, VertexType type, const Vector2D* texcoord)
{
    CopyVertexHelper<Vector2D>::setVertex(getBuffer(index, count), getOffsetOf(type), getVertexSize(), texcoord, count);
    return true;
}

}
